package org.huanmin.common.client;

import com.gitee.huanminabc.utils_common.requestclient.okhttp.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RefreshScope
public class OpenGpt {
    @Value("${open-gpt.text-url}")
    private String textUrl;
    @Value("${open-gpt.api-key}")
    private String  apiKey;


    public    String text(String tips){
        String format = String.format("{\n" +
                "    \"model\": \"gpt-3.5-turbo\",\n" +
                "\n" +
                "    \"messages\": [\n" +
                "      {\n" +
                "        \"role\": \"user\",\n" +
                "        \"content\": \"%s\"\n" +
                "      }\n" +
                "    ]\n" +
                "  }", tips);
        log.info("请求参数:{}",format);
        String sync = OkHttpUtil.builder().
                url(textUrl)
                .addHeader("Authorization", "Bearer  " + apiKey)
                .addParam("JsonBody", format)
                .post("json").sync();

        return sync;
    }

    public String  getApiKey(){
        return apiKey;
    }

}
